<html>
<head><meta charset="utf-8"><title>Iterator::first · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.3A.3Afirst.html">Iterator::first</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="219201431"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%3A%3Afirst/near/219201431" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.3A.3Afirst.html#219201431">(Dec 08 2020 at 12:39)</a>:</h4>
<p>Given an iterator that isn't fused, as in it doesn't yield constant <code>Some</code> but a lot of <code>None</code>s and at some point a <code>Some</code>, what is the best way to get the first existing value out of it? As there is no such thing as Iterator::first. The best I could come up with was to basically wrap the output of it with an additional <code>Some</code> and then <code>flat_map(identitiy)</code> to find the first actual value.<br>
Rough example of the code</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">sol</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">std</span>::<span class="n">iter</span>::<span class="n">from_fn</span><span class="p">(</span><span class="o">||</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nb">Some</span><span class="p">(</span><span class="n">foo</span><span class="p">())</span><span class="w"></span>
<span class="w">    </span><span class="p">})</span><span class="w"></span>
<span class="w">    </span><span class="p">.</span><span class="n">find_map</span><span class="p">(</span><span class="n">std</span>::<span class="n">convert</span>::<span class="n">identity</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">.</span><span class="n">unwrap</span><span class="p">();</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nb">Option</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">;</span><span class="w"> </span><span class="c1">// this produces a lot of `None`s before finally yielding a `Some`</span>
</code></pre></div>
<p>I feel like I'm missing something very obvious right now <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span> but given that most if not all of the iterator consumer functions stop after the first None I wasn't sure if there is a better way.</p>



<a name="219201895"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%3A%3Afirst/near/219201895" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.3A.3Afirst.html#219201895">(Dec 08 2020 at 12:45)</a>:</h4>
<p>yeah, <code>Iterator</code>s which sometimes yield <code>None</code> without being exhausted aren't really idiomatic in rust and aren't really supported too well</p>



<a name="219201998"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%3A%3Afirst/near/219201998" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.3A.3Afirst.html#219201998">(Dec 08 2020 at 12:46)</a>:</h4>
<p>i think using <code>std::iter::from_fn(|| Some(foo())</code> or probably even <code>iter::repeat_with(|| foo())</code> is the easiest way to do this</p>



<a name="219202260"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Iterator%3A%3Afirst/near/219202260" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Iterator.3A.3Afirst.html#219202260">(Dec 08 2020 at 12:49)</a>:</h4>
<p>Oh repeat_with seems a bit better for this, didn't know of that one thanks <span aria-label="thumbs up" class="emoji emoji-1f44d" role="img" title="thumbs up">:thumbs_up:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>